<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class m_sub_kasus extends CI_Model {

    var $sub_kasus_id;
    var $sub_kat_kasus_id;
    var $sub_kasus_nama;
    var $sub_kasus_keterangan;
    var $sub_kasus_oleh;
    var $sub_kasus_dirubah;
    var $cari;
    var $paginate;
    var $num_pages;
    var $offset;

    function __construct() {
        parent::__construct();
        $this->load->library('auth');
    }

    public function get_row_all() {
        if ($this->cari) {
            $this->db->where('(sub_kasus_nama LIKE ' . $this->db->escape('%' . $this->cari . '%') . 'OR sub_kasus_keterangan LIKE ' . $this->db->escape('%' . $this->cari . '%') . ')');
        }

        $query = $this->db->count_all_results('t_sub_kasus');

        return $query;
    }

    public function get() {
        if ($this->cari) {
            $this->db->where('(sub_kasus_nama LIKE ' . $this->db->escape('%' . $this->cari . '%') . 'OR sub_kasus_keterangan LIKE ' . $this->db->escape('%' . $this->cari . '%') . ')');
        }

        if ($this->paginate == TRUE) {
            $query = $this->db->get('t_sub_kasus', $this->num_pages, $this->offset);
            return $query;
        }

        $query = $this->db->get('t_sub_kasus');
        return $query;
    }

    public function get_by_id($id = '0') {
        $this->db->where('sub_kasus_id', $id);
        $query = $this->db->get('t_sub_kasus');

        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return redirect('homes/');
        }
    }

    public function get_sub_kasus_name($name) {
        $this->db->select('sub_kasus_nama');
        $this->db->where('sub_kasus_nama', $name);
        $query = $this->db->get('t_sub_kasus');

        if ($query->num_rows()) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public function get_sub_kasus_from_kat_kasus($kat) {
        $this->db->select('sub_kasus_id, sub_kasus_nama');
        $this->db->where('sub_kat_kasus_id', $kat);
        $query = $this->db->get('t_sub_kasus');

        $sub_kasus = array();
        if ($query->result()) {
            foreach ($query->result() as $val) {
                $sub_kasus[$val->sub_kasus_id] = $val->sub_kasus_nama;
            }
            return $sub_kasus;
        } else {
            return FALSE;
        }
    }

    public function save() {
        if ($this->sub_kasus_nama) {
            $db_array['sub_kasus_nama'] = $this->sub_kasus_nama;
        }

        if ($this->sub_kasus_keterangan) {
            $db_array['sub_kasus_keterangan'] = $this->sub_kasus_keterangan;
        }

        if ($this->sub_kasus_oleh) {
            $db_array['sub_kasus_oleh'] = $this->sub_kasus_oleh;
        }

        if ($this->sub_kasus_dirubah) {
            $db_array['sub_kasus_dirubah'] = $this->sub_kasus_dirubah;
        }
        if ($this->sub_kat_kasus_id) {
            $db_array['sub_kat_kasus_id'] = $this->sub_kat_kasus_id;
        }

        if ($this->sub_kasus_id) {
            $this->db->where('sub_kasus_id', $this->sub_kasus_id);
            $this->db->update('t_sub_kasus', $db_array);
        } else {
            $this->db->insert('t_sub_kasus', $db_array);
        }
    }

    public function delete() {
        $this->db->where('sub_kasus_id', $this->sub_kasus_id);
        $this->db->delete('t_sub_kasus');
    }

}

/* End of file m_sub_kasus.php */
/* Location: ./application/model/m_sub_kasus.php */